package com.example.cinema.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.cinema.bindingdata.Advert;
import com.example.cinema.bindingdata.CarouseData;
import com.example.cinema.bindingdata.LiveTableData;
import com.example.cinema.bindingdata.MovieData;
import com.example.cinema.bindingdata.ShortBindingData;
import com.example.cinema.bindingdata.VipMoneyData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DataBaseMethod {
    private Context context;
    private String tagname;
    private SQLiteDatabase sqlData;

    public DataBaseMethod(Context context, String tagname) {
        this.context = context;
        this.tagname = tagname;
        AppDataBase dataBase = new AppDataBase(context, tagname, null, 2);
        sqlData = dataBase.getWritableDatabase();
    }

    //获取轮播数据
    public List<CarouseData> GetLunBo() {
        List<CarouseData> data = new ArrayList<>();
        Cursor cursor = sqlData.rawQuery("SELECT * FROM poster", null);
        Log.i("UUUU", cursor.getCount() + "");
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                CarouseData cdata = new CarouseData();
                cdata.address = cursor.getString(cursor.getColumnIndex("urls"));

                cdata.news = cursor.getString(cursor.getColumnIndex("apskk"));
                cdata.imgurl = cursor.getString(cursor.getColumnIndex("image"));

                cdata.type = cursor.getInt(cursor.getColumnIndex("types"));

                data.add(cdata);
                cursor.moveToNext();
                Log.i("UUU", "执行文笔");
            }
            cursor.close();
        }
        return data;
    }

    //分类数据
    public List<MovieData> GetTypeData() {
        List<MovieData> data = new ArrayList<>();
        String sql = "SELECT * FROM videotype";
        Cursor cursor = sqlData.rawQuery(sql, null);
        cursor.moveToFirst();
        Log.i("CCC", cursor.getCount() + "");
        while (!cursor.isAfterLast()) {
            MovieData mdata = new MovieData();
            mdata.typename = cursor.getString(cursor.getColumnIndex("typename"));
            mdata.typetags = cursor.getString(cursor.getColumnIndex("typeid"));
            Log.i("III", mdata.typetags);
            data.add(mdata);
            cursor.moveToNext();
        }
        return data;
    }

    //获取公告
    public Map<String, String> GetNotice() {
        Map<String, String> map = new HashMap<>();
        String sql = "SELECT * FROM notice";
        Cursor cursor = sqlData.rawQuery(sql, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Log.i("PPdP", "11111111");
            map.put("title", cursor.getString(0));
            map.put("texts", cursor.getString(1));
            cursor.moveToNext();
        }
        return map;
    }

    //获取直播推荐
    public List<LiveTableData> GetLive() {
        List<LiveTableData> data = new ArrayList<>();
        String sql = "SELECT * FROM livertmp";
        Cursor cursor = sqlData.rawQuery(sql, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Log.i("PPPW", "执行");
            LiveTableData tableData = new LiveTableData();
            tableData.livesid.set(cursor.getString(cursor.getColumnIndex("livesid")));
            tableData.livepath.set(cursor.getString(cursor.getColumnIndex("livepath")));
            tableData.livetype.set(cursor.getString(cursor.getColumnIndex("livetype")));
            tableData.livename.set(cursor.getString(cursor.getColumnIndex("livename")));
            tableData.livestate.set(cursor.getString(cursor.getColumnIndex("livestate")));
            tableData.livecover.set(cursor.getString(cursor.getColumnIndex("livecover")));
            tableData.livetext.set(cursor.getString(cursor.getColumnIndex("livetext")));
            tableData.livetag.set(cursor.getString(cursor.getColumnIndex("livetag")));
            tableData.liverelay.set(cursor.getString(cursor.getColumnIndex("liverelay")));
            data.add(tableData);
            cursor.moveToNext();
        }
        return data;
    }

    //获取短视频数据
    public List<ShortBindingData> GetShortData() {
        List<ShortBindingData> data = new ArrayList<>();
        String sql = "SELECT * FROM videom3u8";
        Cursor cursor = sqlData.rawQuery(sql, null);
        Log.i("TTTT", cursor.getCount() + "");
        Log.i("TTTT", cursor.getColumnCount() + "");
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getCount(); i++) {
            ShortBindingData shortdata = new ShortBindingData();
            shortdata.videoid.set(cursor.getString(cursor.getColumnIndex("videosid")));
            shortdata.videourl.set(cursor.getString(cursor.getColumnIndex("videopath")));
            shortdata.useruid.set(cursor.getString(cursor.getColumnIndex("videousid")));
            shortdata.texts.set(cursor.getString(cursor.getColumnIndex("videobrief")));
            shortdata.imgurl.set(cursor.getString(cursor.getColumnIndex("videouimg")));
            shortdata.playurl.set(cursor.getString(cursor.getColumnIndex("videopart")));
            shortdata.title.set(cursor.getString(cursor.getColumnIndex("videoname")));
            shortdata.image.set(cursor.getString(cursor.getColumnIndex("videocover")));
            data.add(shortdata);
            cursor.moveToNext();
            Log.i("TTTT", "遍历");
        }
        return data;
    }


    //获取充值金额数据
    public List<VipMoneyData> GetVipData()
    {
        List<VipMoneyData> data = new ArrayList<>();
        String sql = "SELECT * FROM vipmoney";
        Cursor cursor = sqlData.rawQuery(sql,null);
        cursor.moveToFirst();
        for (int i=0;i<cursor.getCount();i++)
        {
            VipMoneyData moneyData = new VipMoneyData();
            moneyData.vipmasg.set(cursor.getString(cursor.getColumnIndex("vipmasg")));
            moneyData.viptitle.set(cursor.getString(cursor.getColumnIndex("viptitle")));
            moneyData.vipmoney.set(cursor.getDouble(cursor.getColumnIndex("vipmoney")));
            data.add(moneyData);
            cursor.moveToNext();
        }
        return data;
    }

    //获取播放页广告
    public List<Advert> GetPlayAdv()
    {
        List<Advert> data = new ArrayList<>();
        String sql = "SELECT * FROM adverdata";
        Cursor cursor =  sqlData.rawQuery(sql,null);
        cursor.moveToFirst();
        for (int i=0;i<cursor.getCount();i++)
        {
            Advert advert = new Advert();
            advert.adverurl = cursor.getString(cursor.getColumnIndex("adverurl"));
            advert.adverskip = cursor.getString(cursor.getColumnIndex("adverskip"));
            data.add(advert);
        }
        return data;
    }
    //获取模块页广告
    public List<Advert> GetFramAdv()
    {
        List<Advert> data = new ArrayList<>();
        String sql = "SELECT * FROM adverdatas";
        Cursor cursor =  sqlData.rawQuery(sql,null);
        Log.i("BBBB","获取到的数量"+cursor.getCount());
        cursor.moveToFirst();
        for (int i=0;i<cursor.getCount();i++)
        {
            Advert advert = new Advert();
            advert.adverurl = cursor.getString(cursor.getColumnIndex("adverurl"));
            advert.adverskip = cursor.getString(cursor.getColumnIndex("adverskip"));
            data.add(advert);
        }
        return data;
    }

    /**
     * 获取用户信息
     * @return
     */
    public Map<String,Object> GetUserData()
    {
        Map<String,Object> map = new HashMap<>();
        String sql = "SELECT * FROM userinfo";
        Cursor cursor = sqlData.rawQuery(sql,null);
        cursor.moveToFirst();
        for (int i=0;i<cursor.getCount();i++)
        {
            String uid = cursor.getString(cursor.getColumnIndex("usersid"));
            String psw = cursor.getString(cursor.getColumnIndex("userpswd"));
            String nam = cursor.getString(cursor.getColumnIndex("username"));
            map.put("uid",uid);
            map.put("psw",psw);
            map.put("nam",nam);
        }
        return map;
    }
}
